// This is the main DLL file.
#using <System.dll>
#include <vcclr.h>
#include "IMEEngine.h"

namespace IFELib {

    using namespace System;
    using namespace System::Runtime::InteropServices;

	public ref class IFEClass
	{
    public:
        bool Initialize()
        {
            m_pEngine = new IMEEngine();
            if (m_pEngine == NULL)
            {
                return false;
            }

            if (m_pEngine->SetIMEEngine(L"MSIME.Japan") != S_OK)
            {
                return false;
            }

            return true;
        }

		String ^convert(String ^str,DWORD dwCMode,DWORD dwRequest)
		{
            String ^strRtn = nullptr;

            IntPtr ipInput = Marshal::StringToHGlobalUni(str);
            const WCHAR* szInput = static_cast<const WCHAR*>(ipInput.ToPointer());

            if( m_pEngine->GetJMorphResult( dwRequest, dwCMode, szInput ) == S_OK )
            {
                if( m_pEngine->m_pmorrslt ) 
                {
                    WCHAR* szRtn = m_pEngine->m_pmorrslt->pwchOutput;
                    WORD cchLength = m_pEngine->m_pmorrslt->cchOutput;

                    IntPtr ipRtn(static_cast<void*>(szRtn));
                    strRtn = Marshal::PtrToStringUni(ipRtn, cchLength);
                }

            }

            Marshal::FreeHGlobal(ipInput);
            return strRtn;
		}

		String ^pronToNormal(String ^strPron)
		{
			DWORD dwCMode = FELANG_CMODE_PRECONV | FELANG_CMODE_BESTFIRST;
			DWORD dwRequest = FELANG_REQ_CONV;
			return convert(strPron,dwCMode,dwRequest);
		}

		String ^normalToHiragana(String ^str)
		{
			DWORD dwCMode = FELANG_CMODE_HIRAGANAOUT;
			DWORD dwRequest = FELANG_REQ_REV;
			return convert(str,dwCMode,dwRequest);
		}

    private:
        IMEEngine *m_pEngine;
	};
}